/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logica;

import entities.Alquileres;
import entities.Usuarios;
import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import views.AltaReservas;
import views.EditarAlquileres;
import views.MenuPrincipal;
import views.VistaDisponibilidad;

/**
 *
 * @author root
 */
public class DetalleAlquiler extends javax.swing.JPanel {

    private JFrame frame;
    Alquileres alquilerTemp;
    private static Usuarios usuario;
    private AltaReservas altaReservas;
    private EditarAlquileres editarAlquileres;
    private static int cantidadDetallesCreados = 0;
    public static boolean editado = false;

    /**
     * Creates new form DetalleAlquiler
     */
    public DetalleAlquiler(JFrame frame, Alquileres alquiler, Usuarios usuario) {
        initComponents();
        this.frame = frame;
        this.usuario = usuario;
        alquilerTemp = alquiler;
        setearLabels(alquiler);
        cantidadDetallesCreados++;
        aceptarButton.requestFocus();
        verificarEditarButton(alquilerTemp);
        if ("INVITADO".equals(usuario.getRol().getNombre())) {
            editarAlquilerButton.setEnabled(false);
        }
    }

    public void verificarEditarButton(Alquileres alquilerTemp) {
        if ((alquilerTemp.getTipoAlquiler().equals("C")) || (alquilerTemp.getTipoAlquiler().equals("B"))) {
            editarAlquilerButton.setVisible(false);
        };
    }

    public static int getCantidadDetallesCreados() {
        return cantidadDetallesCreados;
    }

    public static void setCantidadDetallesCreados(int cantidadDetallesCreados) {
        DetalleAlquiler.cantidadDetallesCreados = cantidadDetallesCreados;
    }

    private String tipoAlquiler(String tipo) {
        if (tipo.equals("R")) {
            return "Reserva";
        } else if (tipo.equals("F")) {
            return "Reserva Firme";
        } else {
            return "Ocupado";
        }
    }

    private String formatearFecha(Date fecha) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(fecha);
        String salida = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
        return salida;
    }

    private void setearLabels(Alquileres alquiler) {
        codigoReservaLabel.setText(String.valueOf(alquiler.getIdAlquiler()));
        nombreDepartamentoLabel.setText(alquiler.getIdUnidadAlojamiento().getNombre());
        apellidoNombreLabel.setText(alquiler.getIdCliente().getApellido() + " " + alquiler.getIdCliente().getNombres());
        tipoAlquilerLabel.setText(tipoAlquiler(alquiler.getTipoAlquiler()));
        fechaArriboLabel.setText(formatearFecha(alquiler.getFechaArribo()));
        fechaPartidaLabel.setText(formatearFecha(alquiler.getFechaPartida()));
        montoTotalLabel.setText("$" + alquiler.getMontoTotal());
        montoReservaLabel.setText("$" + alquiler.getMontoReserva());
        cantidadPersonasLabel.setText(String.valueOf(alquiler.getCantidadPersonas()));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        codigoLabel = new javax.swing.JLabel();
        apellidoLabel = new javax.swing.JLabel();
        tipoLabel = new javax.swing.JLabel();
        fechaLabel = new javax.swing.JLabel();
        fechaPLabel = new javax.swing.JLabel();
        montoLabel = new javax.swing.JLabel();
        nombreLabel = new javax.swing.JLabel();
        montoRlabel = new javax.swing.JLabel();
        cantidadLabel = new javax.swing.JLabel();
        codigoReservaLabel = new javax.swing.JLabel();
        nombreDepartamentoLabel = new javax.swing.JLabel();
        apellidoNombreLabel = new javax.swing.JLabel();
        tipoAlquilerLabel = new javax.swing.JLabel();
        fechaArriboLabel = new javax.swing.JLabel();
        fechaPartidaLabel = new javax.swing.JLabel();
        montoTotalLabel = new javax.swing.JLabel();
        montoReservaLabel = new javax.swing.JLabel();
        cantidadPersonasLabel = new javax.swing.JLabel();
        aceptarButton = new javax.swing.JButton();
        editarAlquilerButton = new javax.swing.JButton();

        codigoLabel.setText("Codigo de Reserva:");

        apellidoLabel.setText("Apellido y Nombre:");

        tipoLabel.setText("Tipo de Alquiler:");

        fechaLabel.setText("Fecha Arribo:");

        fechaPLabel.setText("Fecha Partida:");

        montoLabel.setText("Monto Total:");

        nombreLabel.setText("Nombre Departamento:");

        montoRlabel.setText("Monto Reserva:");

        cantidadLabel.setText("Cantidad de Personas:");

        codigoReservaLabel.setFont(new java.awt.Font("Dialog", 0, 11)); // NOI18N
        codigoReservaLabel.setText("*codigo*");

        nombreDepartamentoLabel.setFont(new java.awt.Font("Dialog", 0, 11)); // NOI18N
        nombreDepartamentoLabel.setText("*nombre*");

        apellidoNombreLabel.setFont(new java.awt.Font("Dialog", 0, 11)); // NOI18N
        apellidoNombreLabel.setText("*apellido*");

        tipoAlquilerLabel.setFont(new java.awt.Font("Dialog", 0, 11)); // NOI18N
        tipoAlquilerLabel.setText("*tipo alquiler*");

        fechaArriboLabel.setFont(new java.awt.Font("Dialog", 0, 11)); // NOI18N
        fechaArriboLabel.setText("*fecha Arribo*");

        fechaPartidaLabel.setFont(new java.awt.Font("Dialog", 0, 11)); // NOI18N
        fechaPartidaLabel.setText("*fecha Partida*");

        montoTotalLabel.setFont(new java.awt.Font("Dialog", 0, 11)); // NOI18N
        montoTotalLabel.setText("*monto Total*");

        montoReservaLabel.setFont(new java.awt.Font("Dialog", 0, 11)); // NOI18N
        montoReservaLabel.setText("*monto Reserva*");

        cantidadPersonasLabel.setFont(new java.awt.Font("Dialog", 0, 11)); // NOI18N
        cantidadPersonasLabel.setText("*cantidad Personas*");

        aceptarButton.setBackground(new java.awt.Color(1, 154, 210));
        aceptarButton.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        aceptarButton.setForeground(new java.awt.Color(255, 255, 255));
        aceptarButton.setText("Aceptar");
        aceptarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceptarButtonActionPerformed(evt);
            }
        });

        editarAlquilerButton.setBackground(new java.awt.Color(1, 154, 210));
        editarAlquilerButton.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        editarAlquilerButton.setForeground(new java.awt.Color(255, 255, 255));
        editarAlquilerButton.setText("Editar");
        editarAlquilerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editarAlquilerButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(aceptarButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(55, 55, 55)
                        .addComponent(editarAlquilerButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(codigoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(30, 30, 30))
                            .addComponent(nombreLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(apellidoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(34, 34, 34))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tipoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(52, 52, 52))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(fechaLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(73, 73, 73))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(fechaPLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(64, 64, 64))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(montoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(77, 77, 77))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(montoRlabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(56, 56, 56))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cantidadLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(5, 5, 5)))
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cantidadPersonasLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(montoReservaLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(20, 20, 20))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(montoTotalLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(38, 38, 38))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(fechaPartidaLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(31, 31, 31))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(fechaArriboLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(37, 37, 37))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tipoAlquilerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(39, 39, 39))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(apellidoNombreLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(62, 62, 62))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(nombreDepartamentoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(62, 62, 62))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(codigoReservaLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(69, 69, 69)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(codigoReservaLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(codigoLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(nombreDepartamentoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(nombreLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(apellidoNombreLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(apellidoLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(tipoAlquilerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(tipoLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(fechaArriboLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(fechaLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(fechaPartidaLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(fechaPLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(montoTotalLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(montoLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(montoReservaLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(montoRlabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(cantidadPersonasLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 16, Short.MAX_VALUE))
                    .addComponent(cantidadLabel))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(aceptarButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(editarAlquilerButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(24, 24, 24))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void aceptarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarButtonActionPerformed
        frame.dispose();
        if (editado) {
            EntityManager entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("SIAATPU").createEntityManager();
            //VistaDisponibilidad.frame.dispose();
            //VistaDisponibilidad.main(usuario);
            VistaDisponibilidad.resetear(entityManager);
        }
        cantidadDetallesCreados--;
    }//GEN-LAST:event_aceptarButtonActionPerformed

    private void editarAlquilerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editarAlquilerButtonActionPerformed
        if (alquilerTemp.getTipoAlquiler().equals("R") || alquilerTemp.getTipoAlquiler().equals("F")) {
            if ((MenuPrincipal.frameCreado(altaReservas)) && (MenuPrincipal.frameCerrado(altaReservas.getFrame()))) {
                altaReservas = null;
            }
            if (altaReservas == null) {
                altaReservas = new AltaReservas(usuario, alquilerTemp.getIdAlquiler());
                altaReservas.main(usuario, alquilerTemp.getIdAlquiler());
            } else {
                if (!altaReservas.getFrame().isVisible()) {
                    altaReservas.getFrame().setVisible(true);
                    altaReservas.getFrame().pack();
                }
                altaReservas.getFrame().toFront();
            }
        } else {
            if (editarAlquileres == null) {
                editarAlquileres = new EditarAlquileres(usuario, alquilerTemp.getIdAlquiler());
                editarAlquileres.main(usuario, alquilerTemp.getIdAlquiler());
            } else {
                if (!editarAlquileres.getFrame().isVisible()) {
                    editarAlquileres.getFrame().setVisible(true);
                    editarAlquileres.getFrame().pack();
                }
                editarAlquileres.getFrame().toFront();
            }
        }
    }//GEN-LAST:event_editarAlquilerButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptarButton;
    private javax.swing.JLabel apellidoLabel;
    private javax.swing.JLabel apellidoNombreLabel;
    private javax.swing.JLabel cantidadLabel;
    private javax.swing.JLabel cantidadPersonasLabel;
    private javax.swing.JLabel codigoLabel;
    private javax.swing.JLabel codigoReservaLabel;
    private javax.swing.JButton editarAlquilerButton;
    private javax.swing.JLabel fechaArriboLabel;
    private javax.swing.JLabel fechaLabel;
    private javax.swing.JLabel fechaPLabel;
    private javax.swing.JLabel fechaPartidaLabel;
    private javax.swing.JLabel montoLabel;
    private javax.swing.JLabel montoReservaLabel;
    private javax.swing.JLabel montoRlabel;
    private javax.swing.JLabel montoTotalLabel;
    private javax.swing.JLabel nombreDepartamentoLabel;
    private javax.swing.JLabel nombreLabel;
    private javax.swing.JLabel tipoAlquilerLabel;
    private javax.swing.JLabel tipoLabel;
    // End of variables declaration//GEN-END:variables

    public static void main(Alquileres alq, Usuarios usu) {
        final Alquileres alquiler = alq;
        final Usuarios usuario = usu;
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                JFrame frame = new JFrame();
                frame.setContentPane(new DetalleAlquiler(frame, alquiler, usuario));
                frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                frame.pack();
                frame.setResizable(false);
                frame.setVisible(true);
                frame.setTitle("Detalle de Alquiler");
                frame.setIconImage(new ImageIcon(getClass().getResource("/images/logo.png")).getImage());
                editado = false;
                frame.addWindowListener(new WindowAdapter() {

                    public void windowClosing(WindowEvent e) {
                        DetalleAlquiler.setCantidadDetallesCreados(DetalleAlquiler.getCantidadDetallesCreados() - 1);
                    }
                });
            }
        });
    }
}
